package train48days;

import java.util.Arrays;

public class 手套 {
    public int findMinimum(int n, int[] left, int[] right) {
        int leftzero = 0, rightzero = 0, num = 0;
        for (int i = 0; i < n; i++) {
            if (left[i] == 0) {
                num += right[i];
                right[i]=0;
            } else if (right[i] == 0) {
                num += left[i];
                left[i]=0;
            }
        }
        Arrays.sort(left);
        Arrays.sort(right);
        int rightcount = Arrays.stream(right).sum(), leftcount = Arrays.stream(left).sum();
        int sum = 0;
        for (int i = 0; i < n; i++) {
            if(left[i]==0){
                  leftzero++;
            }
            if (right[i]==0){
                rightzero++;
            }
        }
        if (leftcount <= rightcount) {
            sum = leftcount - left[leftzero] + 1 + num + 1;
        } else if (leftcount > rightcount) {
            sum = rightcount - right[rightzero] + 1 + num + 1;
        }

        return sum;
        //简化后
//        int sum=0;//存放某种颜色的一只手的手套个数为0的时候，另一只手的手套数量
//        int leftSum=0;
//        int rightSum=0;
//        int leftMin=Integer.MAX_VALUE;
//        int rightMin=Integer.MAX_VALUE;
//        for(int i=0;i<n;i++){
//            if(left[i]*right[i]==0){
//                sum=sum+left[i]+right[i];
//            }else{
//                leftSum+=left[i];
//                if(leftMin>left[i]){
//                    leftMin=left[i];
//                }
//                rightSum+=right[i];
//                if(rightMin>right[i]){
//                    rightMin=right[i];
//                }
//            }
//        }
//        return sum+Math.min(leftSum-leftMin+1,rightSum-rightMin+1)+1;
    }
    }
